package com.dxy.yzs.marathon.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxy.yzs.marathon.common.R;
import com.dxy.yzs.marathon.common.exception.util.DateUtil;
import com.dxy.yzs.marathon.domain.Label;
import com.dxy.yzs.marathon.domain.Post;
import com.dxy.yzs.marathon.domain.UserPostRelation;
import com.dxy.yzs.marathon.param.PostProblemParam;
import com.dxy.yzs.marathon.service.LabelService;
import com.dxy.yzs.marathon.service.PostService;
import com.dxy.yzs.marathon.service.UserPostRelationService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * class description
 *
 * @author zhangxunwei
 * @date 2021/10/22
 */
@RestController
@AllArgsConstructor
@RequestMapping("post")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserPostRelationService userPostRelationService;

    @PostMapping("createPostProblem")
    public R createPostProblem(@RequestBody PostProblemParam postProblemParam){
        return R.ok().put("data", postService.createPostProblem(postProblemParam));
    }

    @GetMapping("getCreateProblemsByUserId")
    public R getCreateProblemsByUserId(Integer userId){
        List<Map> postList = postService.getCreateProblemsByUserId(userId).stream().map(po -> {
            Map<String, Object> map = BeanUtil.beanToMap(po);
            Integer replyTimes = (Integer) map.get("replyTimes");
            Date createTime = (Date) map.get("createTime");
            String createTimeStr = DateUtil.showDate(createTime);
            map.put("createTime", createTimeStr);
            map.put("viewTimes", replyTimes * (Math.max(new Random().nextInt(100), 50)) + new Random().nextInt(40));
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("data",postList);
    }

    @GetMapping("/detail")
    public R detail(Integer id) {
        return R.ok().put("data", postService.detail(id));
    }

    @PostMapping("/watch")
    public R watch(@RequestBody Map<String, Integer> params) {
        Integer userId = params.get("userId");
        Integer postId = params.get("postId");

        LambdaQueryWrapper<UserPostRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPostRelation::getPostId, postId);
        queryWrapper.eq(UserPostRelation::getUserId, userId);
        UserPostRelation relation = userPostRelationService.getOne(queryWrapper);

        if (Objects.isNull(relation)) {
            relation = new UserPostRelation();
            relation.setUserId(userId);
            relation.setPostId(postId);

            userPostRelationService.save(relation);
        } else {
            relation.setState(1);
            userPostRelationService.updateById(relation);
        }

        return R.ok();
    }

    @PostMapping("/unwatch")
    public R unwatch(@RequestBody Map<String, Integer> params) {
        Integer userId = params.get("userId");
        Integer postId = params.get("postId");

        LambdaQueryWrapper<UserPostRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPostRelation::getPostId, postId);
        queryWrapper.eq(UserPostRelation::getUserId, userId);
        UserPostRelation relation = userPostRelationService.getOne(queryWrapper);

        if (Objects.nonNull(relation)) {
            relation.setState(0);
            userPostRelationService.updateById(relation);
        }

        return R.ok();
    }

    @GetMapping("/watches")
    public R watches(Integer userId) {
        LambdaQueryWrapper<UserPostRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPostRelation::getUserId, userId);
        queryWrapper.eq(UserPostRelation::getState, 1);
        List<UserPostRelation> relationList = userPostRelationService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(relationList)) {
            List<Integer> postIdList = relationList.stream().map(UserPostRelation::getPostId).collect(Collectors.toList());
            LambdaQueryWrapper<Post> postLambdaQueryWrapper = new LambdaQueryWrapper<>();
            postLambdaQueryWrapper.in(Post::getId, postIdList);

            List<Post> list = postService.list(postLambdaQueryWrapper);

            List<Map> postList = list.stream().map(po -> {
                Map<String, Object> map = BeanUtil.beanToMap(po);
                Integer replyTimes = (Integer) map.get("replyTimes");
                Date createTime = (Date) map.get("createTime");
                String createTimeStr = DateUtil.showDate(createTime);
                map.put("createTime", createTimeStr);
                map.put("viewTimes", replyTimes * (Math.max(new Random().nextInt(100), 50)) + new Random().nextInt(40));
                return map;
            }).collect(Collectors.toList());

            return R.ok().put("data", postList);
        } else {
            return R.ok().put("data", Collections.emptyList());
        }
    }
}
